require 'rextral.rb'

class Difference  
  
	def self.method_missing(method_name,connection)
          method_name_array = method_name.to_s.split("_")
          first_table_name = (method_name_array[(method_name_array.index("for")+1)..(method_name_array.index("and")-1)]).join("_")
          second_table_name = (method_name_array[(method_name_array.index("and")+1)..(method_name_array.index("under")-1)]).join("_")		
          Difference.check_compatibility(first_table_name,connection,second_table_name,connection)
	end
	

  
	def self.check_compatibility(tablename1,connection1,tablename2,connection2)
        table_one_column = []
        table_two_column = []
        compatible_columns_array = []
        name_wise_match = []
        $table_compatibility_array = []
        compatibility_index = 0
        name_compatibility_index = 0
        duplicate_column_match_index = 0
        
        column_count_one = connection1.execute("desc #{tablename1}")
        column_count_two = connection2.execute("desc #{tablename2}")
    
        for column_one in column_count_one	
              table_one_column << column_one
        end
        for column_two in column_count_two	
              table_two_column << column_two
        end    
      
        for column_one in table_one_column
              for column_two in table_two_column
                    column_one_without_name = column_one - (column_one[0].to_a)
                    column_two_without_name = column_two - (column_two[0].to_a)
                    if column_one_without_name == column_two_without_name
                          if compatible_columns_array.flatten.include?column_two
                                previous_match_column = compatible_columns_array[(compatible_columns_array.flatten.index(column_two)-1).to_i]
                                puts "#{column_two} matches with #{column_one} and #{previous_match_column}"
                                duplicate_column_match_index = duplicate_column_match_index + 1
                          end	
                          compatible_columns_array << [column_one,column_two]
                          compatibility_index = compatibility_index + 1
                    end	
              end
        end
      
        for column_one in table_one_column
                for column_two in table_two_column
                      if column_one[0] == column_two[0] 
                            name_wise_match << [column_one,column_two]
                            name_compatibility_index = name_compatibility_index + 1
                      end
                end
        end
      
        puts "For #{tablename1} and #{tablename2}"
        puts 
        name_compatibility_index = (100*name_compatibility_index)/(table_one_column.size)
        puts "The #{name_compatibility_index}% of #{tablename1}\'s columns are compact with columns of #{tablename2}"
        if name_wise_match != nil && !name_wise_match.empty?
              puts "The name wise compatible columns are:"
              for columns in name_wise_match
                    print columns[0][0].to_s+" from "+tablename1+"       "+columns[1][0].to_s+" from "+tablename2
                    puts
                    puts
              end
        end
        
        compatibility_index = (100*compatibility_index)/table_one_column.size
        if compatibility_index > 100
              puts "The two tables are structurally 100% compact"
        else
              puts "The two tables are structurally #{compatibility_index}% compact"
        end	
        if compatible_columns_array != nil && !compatible_columns_array.empty?
              puts "The structure wise compatible columns are:"    
              for columns in compatible_columns_array
                    print columns[0][0].to_s+" from "+tablename1+"       "+columns[1][0].to_s+" from "+tablename2
                    puts
                    puts
              end
        end
        
        duplicate_column_match = (100*duplicate_column_match_index)/table_one_column.size
        puts "For the given tables there is #{duplicate_column_match}% column duplications" 
        puts 
        puts
        if name_compatibility_index == 100 && compatibility_index == 100 && duplicate_column_match == 0
              $table_compatibility_array << [tablename1,tablename2]
              return true
        else
              return false	
        end	
	end	



	
	def self.get_compact_table_for_this_table(tablename,compat_array)
          compat_tables = []
          for table_counter in 0..compat_array.flatten.length
                if compat_array[table_counter] == tablename
                    compat_tables << compat_array[(table_counter-1)] 
                end	  
          end	  
          return compat_tables		
	end	
	
	
	
	
	def self.check_complete_db_compatibility(connection1,connection2)
          all_tables_under_one = connection1.tables
          all_tables_under_two = connection2.tables
          table_one_column = []
          table_two_column = []
          extra_tables = []
          compact_table_count = 0
          for column_one in all_tables_under_one	
                  table_one_column << column_one
          end
          for column_two in all_tables_under_two	
                  table_two_column << column_two
          end     
          if table_two_column.size != table_one_column.size
                  if table_two_column.size > table_one_column.size
                        puts "The databases are not compatible.Second database has #{(table_two_column.size)-(table_one_column.size)} extra tables."	
                  elsif table_two_column.size < table_one_column.size
                        puts "The databases are not compatible.First database  has #{(table_one_column.size)-(table_two_column.size)} extra tables."	
                  end
          else
                table_count = table_two_column.size
                for table_one in table_one_column
                        for table_two in table_two_column
                              compactness = check_compatibility(table_one,connection1,table_two,connection2)
                              if compactness 
                                      compact_table_count = compact_table_count + 1
                              end	
                        end
                        if compact_table_count == table_count
                                puts "The two databases are fully compatible."
                        end	
                end
          end	
	end	

end	

